/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Thomas
 */
import cs1.Keyboard;
public class attack extends ability{
    
    boolean ai;
    public attack(boolean val){
        super("Attack", "Basic Attack.");
        ai = val;
    }
    private int calculateDamage(int atk, int def){
        double dam = atk * (1 - (def * 0.01));
        int ans = (int) dam;
        return ans;
    }
    public void effect(combatObject self, ArrayList<combatObject> Friends, ArrayList<combatObject> Enemies){
        int dam;
        int chooseTarget = 0;
        if(ai){
            Random gen = new Random();
            //Choose a random Target;
            chooseTarget = gen.nextInt(Enemies.size());
            //Check if Enemy is Alive.
            if(!Enemies.get(chooseTarget).isAlive()){
                for(int i = 0; i < Enemies.size(); i++){
                    if(Enemies.get(i).isAlive())
                        chooseTarget = i;
                }
            }
        }else{
            for(int i = 0; i < Enemies.size(); i++){
                System.out.println((i + 1) + ".)");
                Enemies.get(i).printNHpMp();
            }
            chooseTarget = Keyboard.readInt() - 1;
            boolean failsafe = true;
            while(failsafe){
                if(chooseTarget >= Enemies.size() || chooseTarget < 0 || !Enemies.get(chooseTarget).isAlive()){
                    System.out.println("Index out of bounds.");
                    chooseTarget = Keyboard.readInt() - 1;
                    failsafe = true;
                }else{
                    failsafe = false;
                }
            }
            
        }
        
        dam = calculateDamage(self.getAttack(), Enemies.get(chooseTarget).getDefense());
        System.out.println(self.getName() + " damages " + Enemies.get(chooseTarget).getName() + " for " + dam + " damage.\n");
        Enemies.get(chooseTarget).affectHP(dam);
    }
}
